//
//  AppDelegate.m
//  ZhouShi
//
//  Created by 程浪 on 2017/11/22.
//  Copyright © 2019年 程浪V587. All rights reserved.
//

#import "AppDelegate.h"
#import "LoadingPageViewController.h"
#import "LoadDetailsViewController.h"
#import "LoginViewController.h"
#import "SettingViewController.h"
#import "CHLocationManager.h"
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import "HomeRequest.h"


@interface AppDelegate ()

@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    [self setRootViewControllerApplication:application options:launchOptions];

    [[CHLocationManager sharedInstance] startUpdatingLocation];
    [HomeRequest requestUploadPagePointType:@"203" lightat:@""];
    return YES;
}
- (void)setRootViewControllerApplication:(UIApplication *)application options:(NSDictionary *)launchOptions{
    
    LoadingPageViewController *jsViewController = [[LoadingPageViewController alloc] init];
    jsViewController.application = application;
    jsViewController.launchOptions = launchOptions;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:jsViewController];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    
    
}
- (void)loginAgin{
    
//    if (![[PersonalInfo sharedInstance] isLogined]) {
//        return;
//    }
//    ModelLogin *modelLogin = [[PersonalInfo sharedInstance] fetchLoginUserInfo];
//    self.disposableLogin = [[[JJHttpClient new] requestLoginUSERNAME:modelLogin.account andPASSWORD:modelLogin.password andIsChangedPsw:YES] subscribeNext:^(ModelLogin*model) {
//
//    }error:^(NSError *error) {
//        self.disposableLogin = nil;
//    }completed:^{
//        self.disposableLogin = nil;
//    }];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    
    NSString *scheme = [url scheme];
    if ([scheme isEqualToString:@"ch"]) {
        NSString *host =[url host];
        if ([host isEqualToString:@"ruo.ib.a"]) {
            NSString *path = [url path];
            NSString *query = [url query];
            if([path isEqualToString:@"/withme"]){
              
                NSString *type = @"0";
                NSArray *elements = [query componentsSeparatedByString:@"="];
                if (elements.count == 2) {
                    type =  [elements[1] stringByRemovingPercentEncoding];
                }
                [[UIView xhl_getTopViewController].navigationController popToRootViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationBackHomeKey object:@{@"index":@(1),@"type":type}];
                
            }else if([path isEqualToString:@"/cuddle"]){
                // detail
                NSString *lightat = @"";
                NSArray *elements = [query componentsSeparatedByString:@"="];
                if (elements.count == 2) {
                    lightat =  [elements[1] stringByRemovingPercentEncoding];
                }
                LoadDetailsViewController *detailVC = [[LoadDetailsViewController alloc] init];
                detailVC.detailId = lightat;
                [[UIView xhl_getTopViewController].navigationController pushViewController:detailVC animated:YES];
            }else if([path isEqualToString:@"/mean"]){
                // login
                if( [UserInfoModel isLogin]){
                    return YES;
                }
                LoginViewController *vc = [[LoginViewController alloc] init];
                [[UIView xhl_getTopViewController].navigationController pushViewController:vc animated:YES];
            }else if([path isEqualToString:@"/jabbered"]){
                // home
                [[UIView xhl_getTopViewController].navigationController popToRootViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationBackHomeKey object:@{@"index":@(0)}];
            }else if([path isEqualToString:@"/comforted"]){
                // Setting
            
                SettingViewController *vc = [[SettingViewController alloc] init];
                [[UIView xhl_getTopViewController].navigationController pushViewController:vc animated:YES];
            }
        }
        return YES;
    }
    return NO;
}
- (void)applicationWillEnterForeground:(UIApplication *)application{
    [HomeRequest requestUploadPagePointType:@"203" lightat:@""];
}
//-----
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [HomeRequest requestUploadPagePointType:@"204" lightat:@""];
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    if (@available(iOS 14, *)) {
            ATTrackingManagerAuthorizationStatus status = [ATTrackingManager trackingAuthorizationStatus];
            if (status == ATTrackingManagerAuthorizationStatusNotDetermined) {
                [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
                   
                }];
            }
        }
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
